import type { Effect, Reducer } from 'umi';
import { getSelectCompetentAuthority, getSelectBusinessDept } from '@/services/homePage/homePage';
import { message } from 'antd';

export type homePageState = {
  businessDept?: [];
  parkDept?: [];
};

export type HomePageType = {
  namespace: 'homePage';
  state: homePageState;
  effects: {
    initHomePageData: Effect;
  };
  reducers: {
    setInitData: Reducer<homePageState>;
  };
};

const HomePageModel: HomePageType = {
  namespace: 'homePage',
  state: {
    businessDept: [],
    parkDept: [],
  },
  effects: {
    *initHomePageData({ payload }, { call, put }) {
      try {
        const businessDeptResp = yield call(getSelectCompetentAuthority, {
          orgType: 'CompetentAuthority',
        });
        const parkDeptResp = yield call(getSelectCompetentAuthority, {
          orgType: 'Park',
        });
        yield put({
          type: 'setInitData',
          payload: {
            businessDeptResp,
            parkDeptResp,
          },
        });
      } catch (err) {
        message.error('首页初始化失败，请联系管理员').then();
      }
    },
  },
  reducers: {
    setInitData(state, action) {
      const { businessDeptResp, parkDeptResp } = action.payload;
      const businessDept = businessDeptResp.results;
      const parkDept = parkDeptResp.results;
      return {
        ...state,
        businessDept,
        parkDept,
      };
    },
  },
};
export default HomePageModel;
